<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-08-16
 * Time: 17:49
 */

namespace App\Model\Account;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
class SystemType extends Model
{

    public static function listType($where = [], $pageLimit)
    {
        $list = DB::table('system_type')
            ->where($where)
            ->select('st_id', 'st_name', 'st_type_name', DB::raw('from_unixtime(st_create_time) AS st_create_time'),
                DB::raw('from_unixtime(st_update_time) AS st_update_time'))
            ->orderBy('st_id', 'desc')
            ->paginate($pageLimit);
        if (sizeof($list) == 0) {
            return false;
        }
        return $list;
    }


    //添加
    public static function addType($params)
    {
        return DB::table('system_type')->insertGetId([
            'st_name'           =>  $params['st_name'],
            'st_superior_id'    =>  $params['st_superior_id'],
            'st_type_name'      =>  $params['st_type_name'],
            'st_create_time'    =>  time(),
            'st_update_time'    =>  time()
        ]);
    }
    //修改
    public static function editType($params)
    {
        return DB::table('system_type')->where('st_id', $params['st_id'])->update([
            'st_name'           =>  $params['st_name'],
            'st_type_name'      =>  $params['st_type_name'],
            'st_update_time'    =>  time()
        ]);
    }
    //删除
    public static function delType($stId)
    {
        $id = DB::table('system_type')->where('st_superior_id', $stId)->value('st_id');
        if ($id) {
            //存在下级分类 不能删除
            return -1;
        }
        return DB::table('system_type')->where('st_id', $stId)->delete();
    }

    /**根据上级id获取该分类下面的所有种类   《用于下拉框》
     * @param $s_id  上级ID
     * @return mixed
     */
    public static function storageRedis($s_id)
    {
        $list = Redis::get('drop'.$s_id);
        if (!$list) {
            $list = DB::table('system_type')->where('st_superior_id', $s_id)->select('st_id', 'st_name')->get();
            Redis::set('drop'.$s_id, $list);
        }
        if (!is_array($list)) {
            $list = json_decode($list);
        }
        return $list;
    }

    /**
     * @param $st_id
     * @return mixed
     * 获取单个种类名称
     */
    public static function getTypeDetail($st_id){
        $name = Redis::get('name'.$st_id);
        if(!$name){
            $name  = DB::table('system_type')->where('st_id',$st_id)->value('st_name');
            Redis::set('name'.$st_id, $name);
        }
        return $name;
    }

    /**根据用途里面所选择的类型ID    获取该类型下的所有分类
     * @param $stId
     * @return mixed
     */
    public static function getAllTypeForId($stId)
    {
        $list = Redis::get('type'.$stId);
        if (!$list) {
            $list = DB::table('system_type')
                ->where('st_superior_id', $stId)
                ->select('st_id', 'st_name',  'st_type_name')
                ->get();
            Redis::set('type'.$stId, $list);
        }
        if (!is_array($list)) {
            $list = json_decode($list);
        }
        return $list;
    }
}